//首页的小仓库
//获取商品分类的请求的方法:分别暴露
import { reqBanner, reqCategory, reqFloor } from "@/api";
const state = {
  //存储商品分类的字段
  categoryArr: [],
  bannerArr: [],
  floorArr: [],
};
const mutations = {
  GETCATEGORY(state, payload) {
    //将服务器的数据替换state对象空数组
    state.categoryArr = payload;
  },
  GETBANNER(state, paylod) {
    state.bannerArr = paylod;
  },
  GETFLOOR(state, paylod) {
    state.floorArr = paylod;
  },
};
//可以书写逻辑|可以书写异步语句
const actions = {
  async getCategory({ commit, state, dispatch, getters }) {
    //获取商品分类的数据
    //reqCategory方法返回的是一个Promise对象(pending|成功|失败)
    //await等待的是成功的结果
    let result = await reqCategory();
    if (result.code == 200) {
      commit("GETCATEGORY", result.data);
    }
  },
  async getBanner({ commit, state, dispatch, getters }) {
    let result = await reqBanner();
    if (result.code == 200) {
      commit("GETBANNER", result.data);
    }
  },
  async getFloor({ commit, state, dispatch, getters }) {
    let result = await reqFloor();
    if (result.code == 200) {
      commit("GETFLOOR", result.data);
    }
  },
};
const getters = {};
//通过一个对象将四样东西暴露出去
export default {
  state,
  mutations,
  actions,
  getters,
};
